package com.study.talk.module.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.common.util.DateUtil;
import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.GroupUserInfo;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.entity.vo.GroupInfoVo;
import com.study.talk.module.mapper.GroupInfoMapper;
import com.study.talk.module.mapper.GroupUserInfoMapper;
import com.study.talk.module.netty.message.response.GroupMessageResponse;
import com.study.talk.module.service.GroupInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.talk.module.service.GroupUserInfoService;
import com.study.talk.module.service.UserInfoService;
import com.study.talk.module.service.UserService;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author thor
 * @since 2023-12-18
 */
@Service
@Transactional
public class GroupInfoServiceImpl extends ServiceImpl<GroupInfoMapper, GroupInfo> implements GroupInfoService {

    @Resource
    private GroupInfoMapper groupInfoMapper;


    @Resource
    private GroupUserInfoMapper groupUserInfoMapper;

    @Resource
    private UserInfoService userInfoService;


    @Resource
    private UserService userService;

    @Override
    public void addGroup(GroupInfoVo groupInfoVo) {
        GroupInfo groupInfo = new GroupInfo(groupInfoVo);
        groupInfoMapper.insert(groupInfo);
    }

    @Override
    public List<GroupUserInfo> getMembers(Integer groupId) {
        QueryWrapper<GroupUserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupId);
        return groupUserInfoMapper.selectList(wrapper);
    }

    @Override
    public List<GroupInfo> getGroupListByGroupName(String groupName) {
        QueryWrapper<GroupInfo> wrapper = new QueryWrapper<>();
        wrapper.like("name", groupName);
        return list(wrapper);
    }

    @Override
    public List<GroupInfo> getGroupListByUserId(Integer userId) {
        return groupInfoMapper.queryGroupListByUserId(userId);
    }

    @Override
    public List<UserInfo> getMembersUserInfo(Integer groupId) {
        return groupUserInfoMapper.queryMembersUserInfo(groupId);
    }

    @Override
    public FwResult<Boolean> disband(Integer userId, Integer groupId) {
        GroupInfo group = getById(groupId);
        if (group == null) return FwResult.failed(false,"群聊不存在!");
        if (!group.getMainUser().equals(userId)) return FwResult.failed(false,"只有群主可以解散群聊");

        UserInfo mainUser = userInfoService.getById(userId);

        GroupMessageResponse groupMessageResponse = new GroupMessageResponse();
        groupMessageResponse.setMessageType(3);
        groupMessageResponse.setGroupId(groupId);
        groupMessageResponse.setAvatar(mainUser.getAvatar());
        groupMessageResponse.setSendUserName(mainUser.getUsername());
        groupMessageResponse.setSendUserNickName(mainUser.getNickName());
        groupMessageResponse.setSendUserId(mainUser.getId());
        groupMessageResponse.setContent("当前群聊已被解散");
        groupMessageResponse.setDateTime(DateUtil.getCurrentTime());

        List<GroupUserInfo> users = getMembers(groupId);
        if (removeById(groupId)) {
            users.forEach(user -> userService.sendMessage(user.getUserId(),groupMessageResponse));
            return FwResult.ok(true,"操作成功");
        } else {
            return FwResult.failed(false,"操作失败");
        }
    }

    @Override
    public void sendAcceptJoinGroupMsg(GroupInfo groupInfo, UserInfo userInfo) {
        GroupMessageResponse groupMessageResponse = new GroupMessageResponse();
        groupMessageResponse.setMessageType(3);
        groupMessageResponse.setGroupId(groupInfo.getId());
        groupMessageResponse.setAvatar(userInfo.getAvatar());
        groupMessageResponse.setSendUserName(userInfo.getUsername());
        groupMessageResponse.setSendUserNickName(userInfo.getNickName());
        groupMessageResponse.setSendUserId(userInfo.getId());
        groupMessageResponse.setContent(userInfo.getUsername() + "加入群聊");
        groupMessageResponse.setDateTime(DateUtil.getCurrentTime());

        List<GroupUserInfo> members = getMembers(groupInfo.getId());
        for (GroupUserInfo member : members) {
            userService.sendMessage(member.getUserId(), groupMessageResponse);
        }
    }
}
